package com.lhn.netty.netty_demo.inboundhandlerandoutboudhhandler;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @author lhn
 * @date 2025/06/11 19:44
 * @since 1.0.0
 **/
public class MyClientHandler extends SimpleChannelInboundHandler <Long> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Long aLong) throws Exception {
        System.out.println("从服务器" + channelHandlerContext.channel().remoteAddress() + " 读取到long: " + aLong);
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("MyClientHandler.channelRegistered");
        super.channelRegistered(ctx);
    }

    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("MyClientHandler.channelActive");
        //ctx.writeAndFlush(Unpooled.copiedBuffer());
        // 发送一个Long
        ctx.writeAndFlush(123456L);
        //ctx.writeAndFlush(Unpooled.copiedBuffer("abcdabcdabcd", CharsetUtil.UTF_8));
    }
}